import {Component, ElementRef, OnInit, Renderer2, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {DialogService} from "../../../service/dialogService";
import {CCRiskControllerService} from "../../../service/CCRiskController";
import {RestRiskCheckTask} from "../../../entity/RestRiskCheckTask";
import {RestRiskInfo} from "../../../entity/RestRiskInfo";
import {Recheck_RejectData} from "../../../entity/Recheck_RejectData";
import {ModalDirective} from "ngx-bootstrap";
import {UIRiskSubCategory} from "../../../entity/UIRiskSubCategory";
import {WeUIToast} from "angular4-weui";
import {UIRiskReport} from "../../../entity/UIRiskReport";
import {UIRiskCategory} from "../../../entity/UIRiskCategory";

@Component({
  selector: 'app-risk-check-detail',
  templateUrl: './risk-check-detail.component.html',
  styleUrls: ['./risk-check-detail.component.css']
})
export class RiskCheckDetailComponent implements OnInit {

  @ViewChild('taskModal') public taskModal: ModalDirective;
  @ViewChild('toastpop') toastpop: WeUIToast;
  taskId: number;
  // subCategory: UIRiskSubCategory = new UIRiskSubCategory();
  riskCategory: UIRiskCategory = new UIRiskCategory();
  taskDetail: RestRiskCheckTask = new RestRiskCheckTask();
  submitInfo: RestRiskInfo = new RestRiskInfo();
  rejectInfo: Recheck_RejectData = new Recheck_RejectData();

  rejectList: Array<number> = [];
  action: string;
  comment: string;
  code: string; // 二维码扫码的任务详情
  constructor(protected a_router: Router, public element: ElementRef, private renderer: Renderer2,
              public route: ActivatedRoute, public _dialogService: DialogService, public riskService: CCRiskControllerService) {
    this.route.params.subscribe(params => {
      this.taskId = +params['id'];
      this.code = params['code'];
      this.getTaskDetail();
    });
  }

  ngOnInit() {
    this.getCategory();
  }

  getCategory() {
    this.riskService.getCategory(this.taskId).subscribe(
      (items) => {
        this.riskCategory = items;
      }
    );
  }

  getTaskDetail() {
    this.riskService.getTaskDetail(this.taskId).subscribe(
      (detail) => {
        this.taskDetail = detail;
      }
    );
  }

  updateRejectList(event, reportId) {
    if (event.target.checked) {
      this.rejectList.push(reportId);
    } else {
      this.rejectList = this.rejectList.filter((item) => item !== reportId);
    }
    console.log(JSON.stringify(this.rejectList));
  }

  create() {
    if (this.action) {
      this.a_router.navigate(["/dashboard/execution", this.taskId, "report", this.action]);
    } else {
      this.a_router.navigate(["/dashboard/execution", this.taskId, "report", "create"]);
    }
  }

  detail(report: UIRiskReport) {
    if (report.status !== 'finished' && !this.taskDetail.taskType.includes("hospitalModify") && this.taskDetail.riskCheckExecution.status !== 'finished') {
      this.a_router.navigate(["/dashboard/execution", this.taskId, report.subCategory.id, "report", report.id, "update"]);
    } else {
      // this.a_router.navigate(["/dashboard/report", report.id, "detail"]);
      this.a_router.navigate(["/dashboard/execution", this.taskId, report.subCategory.id, "report", report.id, "detail", "finish"]);
    }
  }

  createRiskReport(subCategory: UIRiskSubCategory) {
    if (this.taskDetail.riskCheckExecution.reports.length > 0) {
      this.action = 'create';
    }

    // 扫描位置二维码时 自动填写位置
    if (this.code) {
      this.a_router.navigate(["/dashboard/execution", this.taskId, subCategory.id, "report", this.action, "location", this.taskDetail.checkDetail.dimensionBased.checkTarget, "scan", this.code]);
      return;
    }

    // 二维码任务 自动填写位置
    if (this.taskDetail.checkDetail && this.taskDetail.checkDetail.dimensionBased && this.taskDetail.checkDetail.dimensionBased.checkTarget) {
      // 传递位置信息到隐患报告中
      this.a_router.navigate(["/dashboard/execution", this.taskId, subCategory.id, "report", this.action, "location", this.taskDetail.checkDetail.dimensionBased.checkTarget]);
      return;
    }

    // 部门检查 自动填写位置
    if (this.taskDetail.checkDetail && this.taskDetail.checkDetail.deptBased && this.taskDetail.checkDetail.deptBased.deptName) {
      // 传递位置信息到隐患报告中
      this.a_router.navigate(["/dashboard/execution", this.taskId, subCategory.id, "report", this.action, "location", this.taskDetail.checkDetail.deptBased.deptName]);
      return;
    }

    this.a_router.navigate(["/dashboard/execution", this.taskId, subCategory.id, "report", this.action]);
  }

  submit() {
    switch (this.action) {
      case 'norisk':
        this.withoutRisk();
        break;
      case 'withrisk':
        this.withRisk();
        break;
      case 'complete':
        this.startCheckCompleted();
        break;
      case 'approve':
        this.recheckApprove();
        break;
      case 'reject':
        this.recheckReject();
        break;
    }
  }

  withoutRisk() {
    // 提交前弹窗提示
    this.submitInfo.comment = this.comment;
    this._dialogService.pushDialogMsg({
      showNOButton: true,
      title: "隐患排查",
      content: "您确定排查项目没有隐患吗？",
      btnNOText: "取消",
      btnOKText: "确定"
    }, () => {
      this.riskService.withoutRisk(this.taskId, this.submitInfo).subscribe(
        () => {
          this.refresh();
        },
        (err) => {
          this._dialogService.pushDialogMsg({content: err, btnOKText: "确认"});
        }
      );
    });
  }

  withRisk() {
    if (!this.taskDetail.riskCheckExecution.reports || this.taskDetail.riskCheckExecution.reports.length === 0) {
      this._dialogService.pushDialogMsg({showNOButton: false, content: "至少创建一份隐患报告", btnOKText: "知道了"});
      return;
    }
    this.submitInfo.comment = this.comment;
    this.riskService.withRisk(this.taskId, this.submitInfo).subscribe(
      () => {
        this.refresh();
      }
    );
  }

  startCheckCompleted() {
    this.submitInfo.comment = this.comment;
    this.riskService.startCheckCompleted(this.taskId, this.submitInfo).subscribe(
      () => {
        this.refresh();
      }
    );
  }

  recheckApprove() {
    this.submitInfo.comment = this.comment;
    this.riskService.recheckApprove(this.taskId, this.submitInfo).subscribe(
      () => {
        this.refresh();
      }
    );
  }

  recheckReject() {
    if (this.rejectList.length === 0) {
      this._dialogService.pushDialogMsg({showNOButton: false, content: "至少选择一份隐患报告", btnOKText: "知道了"});
    } else {
      this.taskModal.show();
      // this.rejectInfo.reports = this.rejectList;
      // this.rejectInfo.comment = this.comment;
      // this.riskService.recheckReject(this.taskId, this.rejectInfo).subscribe(
      //   () => {
      //     this.refresh();
      //   }
      // );
    }

  }

  recheckRejectCheck() {
    if (this.rejectList.length === 0) {
      this._dialogService.pushDialogMsg({showNOButton: false, content: "至少选择一份隐患报告", btnOKText: "知道了"});
    } else {
      this.action='reject';
      this.submit();
    }
  }

  refresh() {
    this.taskModal.hide();
    // this.toastpop.show();
    if (this.code) {
      this.a_router.navigate(["dashboard/task", this.code, "qrTaskList"]);
    } else {
      this.a_router.navigate(["/dashboard/task"]);
    }

  }

  deleteReport(report: UIRiskReport) {
    this._dialogService.pushDialogMsg({
      showNOButton: true,
      title: "删除隐患报告",
      content: "您确定要删除该隐患报告吗？",
      btnNOText: "取消",
      btnOKText: "确定"
    }, () => {
      this.riskService.deleteRiskReport(report.id).subscribe(
        (result) => {
          this.taskDetail.riskCheckExecution.reports = this.taskDetail.riskCheckExecution.reports.filter(reportItem => reportItem.id !== report.id);
        }
      );
    });
  }

  submitComment() {
    switch (this.action) {
      case 'reject':
        this.recheckRejectWithComment();
        break;
    }
  }

  recheckRejectWithComment() {
    this.rejectInfo.reports = this.rejectList;
    this.rejectInfo.comment = this.comment;
    this.riskService.recheckReject(this.taskId, this.rejectInfo).subscribe(
      () => {
        this.refresh();
      }
    );
  }

}
